import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from './../../comm/comm.service';
import 'rxjs/add/operator/map';
import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import { ResourceAttribute } from '../../model/config/resource-attribute';
import { DataDictionaryDetail } from '../../model/sys/datadictionary-detail'
declare const $: any;
import { DxDataGridComponent } from '../../../../../node_modules/devextreme-angular';
import { GlobalDataManager } from 'app/ptpmc/comm/global-data-manager';

@Component({
    selector: 'app-resource-attribute',
    templateUrl: './resource-attribute.component.html',
    styleUrls: ['./resource-attribute.component.css'],
})

export class ResourceAttributeComponent implements OnInit, AfterViewInit {

    @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;
    /**查询按类型 */
    public typeItems = [{ id: 1, text: '按值统计' }, { id: 2, text: '按班次统计' }];
    public typeSelected = this.typeItems[0];

    /**选择项 */
    public items: ResourceAttribute[] = [];
    public selectedItems: ResourceAttribute[] = [];


    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public equipmentTypeItems: DataDictionaryDetail[];
    public supplierItems: DataDictionaryDetail[];
    public boolSelectItems: any[];
    public subAareSelected: any;
    constructor(private commServices: CommService) {
    }

    ngOnInit() {
        this.commServices
            .getList(null, ReportUrls.DictDetailUrl + '/EquipmentType')
            .subscribe(model => {
                this.equipmentTypeItems = [];
                if (model.resultCode === 200) {
                    this.equipmentTypeItems = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
        this.commServices
            .getList(null, ReportUrls.DictDetailUrl + '/EquipmentSupplier')
            .subscribe(model => {
                this.supplierItems = [];
                if (model.resultCode === 200) {
                    this.supplierItems = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
        this.boolSelectItems = this.commServices.booleanSelectItems;
        this.getReportData();
    }

    ngAfterViewInit() {
    }

    /**获取表格数据*/
    getReportData(): void {
        const queryModel: QueryModel = new QueryModel();
        const url: string = ReportUrls.ResourceAttributeUrl;
        this.commServices
            .getList(queryModel, url)
            .subscribe(model => {
                this.items = [];
                if (model.resultCode === 200) {
                    this.items = model.data;
                    debugger
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }
    refresh(): void {
        this.getReportData();
    }
    public updateResourceAttributeList: ResourceAttribute[] = [];
    updateResourceAttribute(e) {
        let index: number = this.updateResourceAttributeList.findIndex(u => u.id === e.key.id)
        if (index != -1) {
            this.updateResourceAttributeList[index] = e.key;
        } else {
            this.updateResourceAttributeList.push(e.key)
        }
    }
    /**
     * 保存更新值到数据库
     */
    saveChanges() {
        this.commServices.update(this.updateResourceAttributeList, ReportUrls.ResourceAttributeUpdateUrl)
            .subscribe(model => {
                if (model.resultCode === 200) {
                    this.updateResourceAttributeList = [];
                    alert('更新成功！');
                    // 更新报表显示数据
                    this.getReportData();
                    // 更新成功重新加载全局数据
                    this.commServices.getList(null, ReportUrls.TVResourceUrl).subscribe(model => {
                        if (model.resultCode === 200) {
                            GlobalDataManager.setTVResources(model.data);
                        } else {
                            console.log(model.resultCode + ',' + model.resultMessage);
                        }
                    });
                } else {
                    alert('更新失败！' + model.resultMessage);
                }
            });
    }
    /**获取TimeRange的值*/
    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }

}
